import cv2
import os
import shutil
import tqdm
path=r'../cellseg_nnunet_zigong'
from PIL import Image
movepath=r'/media/alex/柴犬/代码/images'


if not os.path.exists(os.path.join(movepath,'image')):
    os.mkdir(os.path.join(movepath,'image'))
if not os.path.exists(os.path.join(movepath,'mask')):
    os.mkdir(os.path.join(movepath,'mask'))


index = 0
for patient_dir in tqdm.tqdm(os.listdir(path)):
    patient_dir = os.path.join(path,patient_dir)
    for image_name in os.listdir(os.path.join(patient_dir,'train')):
        image_path = os.path.join(patient_dir,'train',image_name)
        mask_path = os.path.join(patient_dir,'trainmask',image_name)
        image = Image.open(image_path)
        image = image.convert("RGB")

        save_name = '%04d.png'%index
        image_path_save = os.path.join(movepath,'image',save_name)
        mask_path_save = os.path.join(movepath,'mask',save_name)
        image.save(image_path_save)
        shutil.copy(mask_path,mask_path_save)
        index+=1